package thesis.javaclass;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.NoSuchElementException;
import java.util.Scanner;

import sun.print.resources.serviceui_zh_TW;

public class BroadcastServer implements Runnable {
	private static InetAddress host;
	private int port = -1;
	private Socket socket = null;
	private String imei = "";
	private ServiceRequest serviceRequest = null;
	private String numberPlate;

	public BroadcastServer(int port, String imei, String numberPlate,
			ServiceRequest serviceRequest) {
		this.port = port;
		this.imei = imei;
		this.serviceRequest = serviceRequest;
		this.numberPlate = numberPlate;
	}

	private void accessServer() {

		try {
			Scanner input = new Scanner(socket.getInputStream());
			PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
			System.out.println("Connected");
			String response;

			String info = serviceRequest.getPhoneNumber() + ";"
					+ serviceRequest.getAddress() + ";"
					+ serviceRequest.getCoordinate().get(0) + ";"
					+ serviceRequest.getCoordinate().get(1);
			output.println(info);
			System.out.println("Send data ....");

			response = input.nextLine();
			if (response.equals(StringConstant.OK)) {
				System.out.println(response);
				serviceRequest.addAcceptImei(imei);
				serviceRequest.addAcceptedTaxiNumber(numberPlate);

				while (true) {// wait until finished flag = true
					try {
						Thread.sleep(100);
						if (serviceRequest.isFinished()) {
							if (serviceRequest.getAcceptTaxiImeis().get(0)
									.equals(imei)) {
								output.println(StringConstant.OK);
								response = input.nextLine();
								System.out.println(response);
								if (response.equals(StringConstant.CANCEL))
								{
									serviceRequest.getAcceptTaxiImeis().clear();
									ServiceList.setFree(imei);
								}
								else {
									ConfirmTaxi confirmSer = new ConfirmTaxi(
											imei,
											serviceRequest.getPhoneNumber(),
											StatusCode.ACCEPT_STATUS);
									confirmSer.run();
								}

							} else {
								output.println(StringConstant.CANCEL);
								ServiceList.setFree(imei);
							}
							break;
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			} else {
				System.out.println(response);
				ServiceList.setFree(imei);
			}

		} catch (IOException ioEx) {
			System.out.println("No server listen");

		} catch (NoSuchElementException nsee) {
			nsee.printStackTrace();
			ServiceList.setFree(imei);
		} finally {
			if (socket != null) {
				try {

					System.out.println("\n* Closing connection... *:" + imei);
					socket.close();
				} catch (IOException ioEx) {
					System.out.println("Unable to disconnect!");
					System.exit(1);
				}
			}
		}
	}

	@Override
	public void run() {
		try {
			host = InetAddress.getLocalHost();
			socket = new Socket(host, port);

		} catch (UnknownHostException uhEx) {
			System.out.println("Host ID not found!");
			System.exit(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		accessServer();

	}
}
